package com.zoz.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zoz.constant.CommonConstant;
import com.zoz.constant.DatabaseConstant;
import com.zoz.service.DatabaseService;
import com.zoz.vo.DatabaseVo;
/**
 * 数据库调用实现
 *@author zhuhg
 *@date 2020年6月4日 下午3:13:21
 */
@Service
public class DatabaseServiceImpl implements DatabaseService {

	 private static final Logger logger = LoggerFactory.getLogger(DatabaseServiceImpl.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public String queryAllDatabase() {
		JSONObject json = new JSONObject();
        try { 
        		List<String> datas = jdbcTemplate.queryForList(DatabaseConstant.DATABASE_ALL_SQL, String.class);
        		
        		json.put(CommonConstant.DATA,datas);
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_Y);
        	} catch (Exception e) { 
        		logger.info("查询数据库 失败: {}", e.getMessage(),e);
        		json.put(CommonConstant.MESSAGE,e.getMessage());
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_N);
        	}
        return json.toJSONString();
	}

	@Override
	public String queryAllTables(DatabaseVo vo) {
		JSONObject json = new JSONObject();
        try { 
        		List<Map<String, Object>> datas = jdbcTemplate.queryForList(DatabaseConstant.TABLE_ALL_SQL,vo.getDatabase());
        		json.put(CommonConstant.DATA,datas);
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_Y);
        	} catch (Exception e) { 
        		logger.info("查询{}数据库所有表 失败: {}",vo.getDatabase() ,e.getMessage(),e);
        		json.put(CommonConstant.MESSAGE,e.getMessage());
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_N);
        	}
        return json.toJSONString();
	}

	@Override
	public String queryAllTableColumns(DatabaseVo vo) {
		JSONObject json = new JSONObject();
        try { 
        	List<Map<String, Object>> datas = jdbcTemplate.queryForList(DatabaseConstant.TABLE_COLUM_TYPE_SQL,vo.getTableName(),vo.getDatabase());
        		json.put(CommonConstant.DATA,datas);
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_Y);
        	} catch (Exception e) { 
        		logger.info("查询{}数据库{}表 列属性 失败: {}",vo.getDatabase() ,e.getMessage(),e);
        		json.put(CommonConstant.MESSAGE,e.getMessage());
        		json.put(CommonConstant.STATUS,CommonConstant.STATUS_N);
        	}
        return json.toJSONString();
	}

	@Override
	public String queryCurDatabaseTable(String database, String table) {
		List<Map<String, Object>> datas = jdbcTemplate.queryForList(DatabaseConstant.TABLE_COLUM_TYPE_SQL,table,database);
		return JSON.toJSONString(datas);
	}
	
	
}
